import DateUtil from "./DateUtil";

export default class ServerTimer {

    private static serverTime: number = 0; //单位毫秒
    private static localTime: number = 0


    public static get now() {
        let now: number = DateUtil.now()
        if (this.serverTime == 0) {
            return now
        } else {
            let diffTime: number = now - this.localTime;
            return this.serverTime + diffTime;
        }
    }

     //服务器返回的是秒
     public static setServerTime(serverTime: number) {
        this.localTime = DateUtil.now();
        this.serverTime = serverTime;
    }

    public static isToday(dateTime:number):boolean{
        let nowDate: Date = new Date(ServerTimer.now);
        let checkDate: Date = new Date(dateTime);
        if (checkDate.getFullYear() == nowDate.getFullYear() && 
            checkDate.getMonth() == nowDate.getMonth() && 
            checkDate.getDate() == nowDate.getDate()) {
            return true;
        }
        return false;
    }

    public static delayCall(delayTime: number = 1000, component: cc.Component): Promise<void> {
        return new Promise((resolve, reject) => {
            component.scheduleOnce(() => {
                resolve();
            }, delayTime / 1000)
        })
    }

}